<template>
  <div class="user-info" v-loading.fullscreen="loading">
    <el-card class="basicInfo-pannel">
      <div class="input-avatar">
        <img class="avatar" :src="avatarUrl" />
      </div>
      <p class="username">用户名:{{ userInfo.username }}</p>
      <p class="userType">{{ userType }}</p>
    </el-card>
    <el-card class="addInfo-pannel">
      <p class="addInfo-label">基本信息</p>
      <el-divider class="divider"></el-divider>
      <div class="message-pannel">
        <div class="pannel-left">
          <label class="field">用户ID</label>
          <label class="field">发表文章数</label>
          <label class="field">邮箱</label>
        </div>
        <div class="pannel-right">
          <p class="field-value">{{ userId }}</p>
          <p class="field-value">{{ articleInfo.total }}</p>
          <p class="field-value">{{ userInfo.email }}</p>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script>
import { api } from "@/api";
export default {
  data() {
    return {
      articleInfo: {
        list: [],
        total: 0,
      },
      loading: false,
      userInfo: {
        id: 0,
        username: "",
        admin: false,
        email: "",
      },
    };
  },
  methods: {
    async loadUserInfo(username) {
      this.loading = true;
      try {
        this.userInfo = (
          await this.$axios(api.user.getUserInfo(username))
        ).data;
        this.loading = false;
      } catch (error) {
        this.loading = false;
        if (error.response) {
          this.$alert(error.response.data.message);
        } else {
          this.$alert("网络错误");
        }
      }
    },
    async loadArticleInfo(aid = null) {
      try {
        this.loading = true;
        this.articleInfo = (
          await this.$axios(api.article.getArticleList(aid))
        ).data;
        this.loading = false;
      } catch (error) {
        this.loading = false;
        if (error.response.data.code == 403) {
          this.$router.push("/login");
        } else if (error.response) {
          this.$alert(error.response.data.message);
        } else {
          this.$alert("网络错误");
        }
      }
    },
  },
  computed: {
    userType() {
      return this.userInfo
        ? this.userInfo.admin
          ? "管理员"
          : "普通用户"
        : "未登录";
    },
    userId() {
      return this.userInfo ? this.userInfo.id : null;
    },
    avatarUrl() {
      return "/api/" + api.user.getAvatar(this.userInfo.username).url;
    },
  },
  async mounted() {
    await this.loadUserInfo(this.$route.query.username);
    await this.loadArticleInfo(this.userInfo.id);
  },
};
</script>

<style lang="less" scoped>
.user-info {
  margin-top: 60px;
  .basicInfo-pannel {
    margin: 0 auto;
    width: 60%;
    height: 150px;
    min-width: 200px;
    .userType {
      color: darkgray;
      position: absolute;
      margin: 60px 200px;
    }
    .username {
      position: absolute;
      margin: 10px 200px;
      font-size: 20px;
      font-weight: bold;
    }
    .input-avatar {
      position: absolute;
      width: 56%;
      height: 180px;
      min-width: 200px;
      .avatar {
        width: 100px;
        height: 100px;
        border-radius: 50%;
        margin: 0 50px;
        position: absolute;
      }
    }
  }
  .addInfo-pannel {
    transform: translate(0, 20px);
    margin: 0 auto;
    width: 60%;
    height: 300px;
    min-width: 200px;
    .message-pannel {
      display: flex;
      .pannel-left {
        flex: 1;
        .field {
          margin: 40px 40px;
          display: block;
        }
      }
      .pannel-right {
        flex: 1;
        flex-direction: row;
        .field-value {
          font-weight: bold;
          margin: 42px 0;
        }
      }
    }
    .divider {
      margin: 5px 0;
    }
    .addInfo-label {
      font-weight: bold;
      font-size: 16px;
      margin: 0;
    }
  }
}
</style>
